package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.QuestionAnswer;
import com.think.teaching.service.impl.QuestionAnswerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 参考答案 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "问题参考答案管理接口")
public class QuestionAnswerController {

	private final QuestionAnswerServiceImpl questionAnswerService;

	public QuestionAnswerController(QuestionAnswerServiceImpl questionAnswerService) {
		this.questionAnswerService = questionAnswerService;
	}

	@ApiOperation("添加新的试题参考答案")
	@PostMapping("admin/questionAnswer")
	public Result AddNewQuestionAnswer(@RequestBody() QuestionAnswer data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			QuestionAnswer questionAnswer = questionAnswerService.AddQuestionAnswer(data);
			if (questionAnswer != null) {
				return Result.success(questionAnswer);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新试题参考答案")
	@RequestMapping(value = "admin/questionAnswer", method = RequestMethod.PUT)
	public Result UpdateNewQuestionAnswer(@RequestBody() QuestionAnswer data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			QuestionAnswer questionAnswer = questionAnswerService.UpdateQuestionAnswer(data);
			if (questionAnswer != null) {
				return Result.success(questionAnswer);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除试题参考答案")
	@RequestMapping(value = "admin/questionAnswer/{id}", method = RequestMethod.DELETE)
	public Result DeleteNewQuestionAnswer(@PathVariable("id") Integer id) {
		QuestionAnswer questionAnswer = questionAnswerService.getById(id);
		if (questionAnswer == null) {
			return Result.failure(ResultCode.QUESTION_ANSWER_NOT_EXIST);
		} else {
			if (questionAnswerService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("获取试题参考答案详情")
	@RequestMapping(value = "admin/questionAnswer/{id}", method = RequestMethod.GET)
	public Result GetQuestionAnswerById(@PathVariable("id") Integer id) {
		QuestionAnswer questionAnswer = questionAnswerService.getById(id);
		if (questionAnswer == null) {
			return Result.failure(ResultCode.QUESTION_ANSWER_NOT_EXIST);
		} else {

			return Result.success(questionAnswer);
		}
	}

	@ApiOperation("管理员修改试题参考答案状态")
	@RequestMapping(value = "admin/questionAnswer/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateQuestionAnswerState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		QuestionAnswer questionAnswer = questionAnswerService.getById(id);
		if (questionAnswer == null) {
			return Result.failure(ResultCode.QUESTION_ANSWER_NOT_EXIST);
		} else {
			try {
				questionAnswer = questionAnswerService.SetQuestionAnswerState(id, state);
				return Result.success(questionAnswer);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
}

